var fs = require('fs')
var path = require('path')

var myCopy = {
	copy(from, to) {
		var self = this;
		const fromPath = path.resolve(from)
		const toPath = path.resolve(to)
		fs.access(toPath, function (err) {
			if (err) {
				fs.mkdirSync(toPath)
			}
		})
		
		fs.readdir(fromPath, function (err, paths) {
			if (err) {
				Editor.log(err)
				return
			}
			paths.forEach(function (item) {
				const newFromPath = fromPath + '/' + item
				const newToPath = path.resolve(toPath + '/' + item)

				fs.stat(newFromPath, function (err, stat) {
					if (err) return
					if (stat.isFile()) {
						self.copyFile(newFromPath, newToPath)
						Editor.log(newToPath)
					}
					if (stat.isDirectory()) {
						self.copy(newFromPath, newToPath)
					}
				})
			})
		})
	},

	copyFile(from, to) {
		fs.copyFileSync(from, to, function (err) {
			if (err) {
				Editor.log(err)
				return
			}
		})
	}
}

module.exports = myCopy;
